package com.example.evaluate.controller;

import com.example.evaluate.model.CallBackMsg;
import com.example.evaluate.model.Question;
import com.example.evaluate.model.Student;
import com.example.evaluate.model.TrueTeacher;
import com.example.evaluate.model.temp.GradeDistribution;
import com.example.evaluate.model.temp.TeacherInfo;
import com.example.evaluate.service.TeacherService;
import com.example.evaluate.service.TempService;
import com.example.evaluate.util.AjaxUtil;
import com.example.evaluate.util.CalculateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
public class TeacherController {
    @Autowired
    TeacherService teacherService;

    @Autowired
    TempService tempService;

    /*
     * 处理老师的登陆
     * */
    @RequestMapping("teacherLogin")
    public void updateScenic(HttpServletResponse response, HttpServletRequest request){
        int teacherId = Integer.parseInt(request.getParameter("teacherId"));
        String teacherPassword = request.getParameter("teacherPassword");
        System.out.println("teacherId,password:"+teacherId+teacherPassword);

        TrueTeacher trueTeacher = new TrueTeacher(teacherId,teacherPassword);
        boolean flag = teacherService.login(trueTeacher);

        CallBackMsg msg = new CallBackMsg(flag?"success":"failed");
        System.out.println(msg.toString());

        AjaxUtil.writeJSON2Response(msg.toString(),response);
    }


    /*
     * 修改密码
     *
     * */
    @RequestMapping("updateTeacherPassword")
    public void updateTeacherPassword(HttpServletResponse response, HttpServletRequest request){
        int trueTeacherId = Integer.parseInt(request.getParameter("trueTeacherId"));
        String teacherPassword = request.getParameter("teacherPassword");

        System.out.println(trueTeacherId+teacherPassword);
        boolean flag = teacherService.updatePassword(new TrueTeacher(trueTeacherId,teacherPassword));


        CallBackMsg msg = new CallBackMsg(flag?"success":"fail");
        System.out.println(msg.toString());

        AjaxUtil.writeJSON2Response(msg.toString(),response);
    }


  /**
   * 评教率
   *
   * */
  @RequestMapping("getEvaluationRateByCourseName")
  public void getEvaluationRateByCourseName(HttpServletResponse response, HttpServletRequest request){
      String courseName = request.getParameter("courseName");
      int a = teacherService.getEvaluatedNumberByCourseName(courseName);
      int b =teacherService.getTotalStudentNumberByCourseName(courseName);

      if(a==0||b==0){
          CallBackMsg msg = new CallBackMsg(0+"");
          AjaxUtil.writeJSON2Response(msg.toString(),response);
      }else {
          double temp = ((double)a/(double) b) * 100;
          double res = Double.parseDouble(String.format("%.2f",temp));
          AjaxUtil.writeJSON2Response((new CallBackMsg(res+"")).toString() ,response);
      }

  }

  /**
   * 等第分布状况
   * */
  @RequestMapping("getGradeDistribution")
  public void getGradeDistribution(HttpServletResponse response, HttpServletRequest request){
      String courseName = request.getParameter("courseName");
      List<Question> questions = teacherService.findQuestionByCourseName(courseName);

      List<GradeDistribution> distributions = new ArrayList<>();
      GradeDistribution d0 = new GradeDistribution("优");
      GradeDistribution d1 = new GradeDistribution("良");
      GradeDistribution d2 = new GradeDistribution("中");
      GradeDistribution d3 = new GradeDistribution("及格");
      GradeDistribution d4 = new GradeDistribution("差");


      for (Question q:
           questions) {
          switch (q.getQuestion1()){
              case 0:
                  d0.setGradeNumber(d0.getGradeNumber()+1);
                  break;
              case 1:
                  d1.setGradeNumber(d1.getGradeNumber()+1);
                  break;
              case 2:
                  d2.setGradeNumber(d2.getGradeNumber()+1);
                  break;
              case 3:
                  d3.setGradeNumber(d3.getGradeNumber()+1);
                  break;
              case 4:
                  d4.setGradeNumber(d4.getGradeNumber()+1);
                  break;
          }

          switch (q.getQuestion2()){
              case 0:
                  d0.setGradeNumber(d0.getGradeNumber()+1);
                  break;
              case 1:
                  d1.setGradeNumber(d1.getGradeNumber()+1);
                  break;
              case 2:
                  d2.setGradeNumber(d2.getGradeNumber()+1);
                  break;
              case 3:
                  d3.setGradeNumber(d3.getGradeNumber()+1);
                  break;
              case 4:
                  d4.setGradeNumber(d4.getGradeNumber()+1);
                  break;
          }

          switch (q.getQuestion3()){
              case 0:
                  d0.setGradeNumber(d0.getGradeNumber()+1);
                  break;
              case 1:
                  d1.setGradeNumber(d1.getGradeNumber()+1);
                  break;
              case 2:
                  d2.setGradeNumber(d2.getGradeNumber()+1);
                  break;
              case 3:
                  d3.setGradeNumber(d3.getGradeNumber()+1);
                  break;
              case 4:
                  d4.setGradeNumber(d4.getGradeNumber()+1);
                  break;
          }

          switch (q.getQuestion4()){
              case 0:
                  d0.setGradeNumber(d0.getGradeNumber()+1);
                  break;
              case 1:
                  d1.setGradeNumber(d1.getGradeNumber()+1);
                  break;
              case 2:
                  d2.setGradeNumber(d2.getGradeNumber()+1);
                  break;
              case 3:
                  d3.setGradeNumber(d3.getGradeNumber()+1);
                  break;
              case 4:
                  d4.setGradeNumber(d4.getGradeNumber()+1);
                  break;
          }

          switch (q.getQuestion5()){
              case 0:
                  d0.setGradeNumber(d0.getGradeNumber()+1);
                  break;
              case 1:
                  d1.setGradeNumber(d1.getGradeNumber()+1);
                  break;
              case 2:
                  d2.setGradeNumber(d2.getGradeNumber()+1);
                  break;
              case 3:
                  d3.setGradeNumber(d3.getGradeNumber()+1);
                  break;
              case 4:
                  d4.setGradeNumber(d4.getGradeNumber()+1);
                  break;
          }

          switch (q.getQuestion6()){
              case 0:
                  d0.setGradeNumber(d0.getGradeNumber()+1);
                  break;
              case 1:
                  d1.setGradeNumber(d1.getGradeNumber()+1);
                  break;
              case 2:
                  d2.setGradeNumber(d2.getGradeNumber()+1);
                  break;
              case 3:
                  d3.setGradeNumber(d3.getGradeNumber()+1);
                  break;
              case 4:
                  d4.setGradeNumber(d4.getGradeNumber()+1);
                  break;
          }

          switch (q.getQuestion7()){
              case 0:
                  d0.setGradeNumber(d0.getGradeNumber()+1);
                  break;
              case 1:
                  d1.setGradeNumber(d1.getGradeNumber()+1);
                  break;
              case 2:
                  d2.setGradeNumber(d2.getGradeNumber()+1);
                  break;
              case 3:
                  d3.setGradeNumber(d3.getGradeNumber()+1);
                  break;
              case 4:
                  d4.setGradeNumber(d4.getGradeNumber()+1);
                  break;
          }

          switch (q.getQuestion8()){
              case 0:
                  d0.setGradeNumber(d0.getGradeNumber()+1);
                  break;
              case 1:
                  d1.setGradeNumber(d1.getGradeNumber()+1);
                  break;
              case 2:
                  d2.setGradeNumber(d2.getGradeNumber()+1);
                  break;
              case 3:
                  d3.setGradeNumber(d3.getGradeNumber()+1);
                  break;
              case 4:
                  d4.setGradeNumber(d4.getGradeNumber()+1);
                  break;
          }

          switch (q.getQuestion9()){
              case 0:
                  d0.setGradeNumber(d0.getGradeNumber()+1);
                  break;
              case 1:
                  d1.setGradeNumber(d1.getGradeNumber()+1);
                  break;
              case 2:
                  d2.setGradeNumber(d2.getGradeNumber()+1);
                  break;
              case 3:
                  d3.setGradeNumber(d3.getGradeNumber()+1);
                  break;
              case 4:
                  d4.setGradeNumber(d4.getGradeNumber()+1);
                  break;
          }

          switch (q.getQuestion10()){
              case 0:
                  d0.setGradeNumber(d0.getGradeNumber()+1);
                  break;
              case 1:
                  d1.setGradeNumber(d1.getGradeNumber()+1);
                  break;
              case 2:
                  d2.setGradeNumber(d2.getGradeNumber()+1);
                  break;
              case 3:
                  d3.setGradeNumber(d3.getGradeNumber()+1);
                  break;
              case 4:
                  d4.setGradeNumber(d4.getGradeNumber()+1);
                  break;
          }
      }

      distributions.add(d0);
      distributions.add(d1);
      distributions.add(d2);
      distributions.add(d3);
      distributions.add(d4);

      AjaxUtil.writeJSON2Response(distributions.toString(),response);

  }

  /**
   * 获评分数区间的学生人数
   * */
  @RequestMapping("getEvaluateRangeStudentNumber")
  public void getEvaluateRangeStudentNumber(HttpServletResponse response, HttpServletRequest request){
      String courseName = request.getParameter("courseName");
      List<Question> questions = teacherService.findQuestionByCourseName(courseName);
      List<Integer> res = new ArrayList<>();
      int n1 = 0,n2 = 0,n3 = 0,n4 = 0,n5 = 0;

      for (Question q:
           questions) {
          int totalPoint = CalculateUtil.getTotalPoint(q);
          System.out.println("打印分数"+totalPoint);

          if (totalPoint >= 0&&totalPoint <= 20)
              n1++;
          else if(totalPoint > 20&&totalPoint <= 40)
              n2++;
          else if(totalPoint > 40&&totalPoint <= 60)
              n3++;
          else if(totalPoint > 60&&totalPoint <= 80)
              n4++;
          else
              n5++;
      }
      res.add(n1);
      res.add(n2);
      res.add(n3);
      res.add(n4);
      res.add(n5);

      AjaxUtil.writeJSON2Response(res.toString(),response);
  }

  /**
   * 获取老师所有信息
   * */
  @RequestMapping("getTrueTeacherAllInfo")
  public void getTrueTeacherAllInfo(HttpServletResponse response, HttpServletRequest request){
      int trueTeacherId = Integer.parseInt(request.getParameter("trueTeacherId"));
      TeacherInfo teacherInfo = tempService.getTeacherInfoByTrueTeacherId(trueTeacherId);

      AjaxUtil.writeJSON2Response(teacherInfo.toString(),response);
  }


}
